package com.leff.cards.request;

import com.leff.cards.Dictionary;
import com.leff.cards.StoreWrapper;
import com.leff.cards.entity.Group;
import com.leff.cards.entity.User;

public class CreateGroup extends Request {

	public CreateGroup(Dictionary dict) {
		super(dict);
	}

	public RequestResult doCreate() {
		
		RequestResult res = this.verifyRequiredParameters(Parameter.NAME, Parameter.ID);
		if(!res.isSuccessful()) {
			return res;
		}
		
		String name = (String)mDictionary.get(Parameter.NAME);
		String idStr = (String)mDictionary.get(Parameter.ID);
		long id = Long.parseLong(idStr);
		
		// Find the user
		User U = StoreWrapper.getUserById(id);
		if(U == null) {
			res.setErrorResult("No user with id " + idStr + " found in datastore");
			return res;
		}
		
		// Create group
		Group G = new Group(name, U);
		
		// Store it
		StoreWrapper.store(G);
		
		res.setSuccessMessage("Group '" + name + "' was successfully created.");
		
		return res;
	}
}
